package com.zjy.spts.controller;

import com.zjy.spts.bean.UserBean;
import com.zjy.spts.service.UserService;
import com.zjy.spts.util.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by zjy629 on 2017/11/22.
 */
@Controller
@RequestMapping
public class UserController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;
    private JSONUtil jsonUtil = new JSONUtil();

    /**
     * 注册
     */
    @RequestMapping("/register")
    public String register(){
        return "register";
    }
    @ResponseBody
    @RequestMapping(value="/user/register")
    public JSONUtil register(HttpServletRequest request){
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        UserBean userBean = new UserBean();
        userBean.setName(name);
        userBean.setPassword(password);
        int i = userService.insertUser(userBean);
        if(i>=1){
          HttpSession session = request.getSession();
          session.setAttribute("name",name);
          session.setAttribute("psw",password);
            // System.out.println();
            jsonUtil.setMessCode("00000");
            String mm =jsonUtil.messages("registerSuccess");
            jsonUtil.setMessage(mm);
           // logger.info(jsonUtil.getMessCode());
           // logger.error(mm);
            return jsonUtil;
        }else{
            jsonUtil.setMessCode("011111");
            jsonUtil.setMessage("注冊失敗");
            return jsonUtil;
        }
    }

    /**
     * 登录
     */
    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @ResponseBody
    @RequestMapping(value="/user/login")
    public JSONUtil login(HttpServletRequest request){
        String name = request.getParameter("name");
       // logger.info("name:"+name);
        String password = request.getParameter("password");
        UserBean userBean = userService.findByName(name);
        if(userBean != null){
            //有此用户
            if(userBean.getPassword().equals(password)){
                //登录成功
                jsonUtil.setMessCode("00000");
                String mm =jsonUtil.messages("loginSuccess");
                jsonUtil.setMessage(mm);
                HttpSession session = request.getSession();
                String userName  = (String)session.getAttribute("name");
                if(userName == null || ("").equals(userName)){
                    session.setAttribute("name",name);
                }
               // session.setAttribute("name",name);
               // session.setAttribute("psw",password);
            }else{
                //密码错误
                jsonUtil.setMessCode("00002");
                jsonUtil.setMessage("密码错误请重新输入");
            }
        }else{
            jsonUtil.setMessCode("00001");
            jsonUtil.setMessage("无此用户");
        }
            return jsonUtil;
    }


   /* @RequestMapping(value="/user/index")
    public String index(HttpServletRequest request, UserBean userBean){
        //将name属性传入到user_index.jsp页面中展示
        request.setAttribute("name",userBean.getName());
        System.out.println("============================"+userBean.getName());
        //跳转user_index.jsp页面
        return "user_index";
    }*/












    @RequestMapping(value="/user/index")
    public String index(HttpServletRequest request, UserBean userBean){
        //将name属性传入到user_index.jsp页面中展示
        request.setAttribute("name",userBean.getName());
        System.out.println("============================"+userBean.getName());
        //跳转user_index.jsp页面
        return "user_index";
    }

    @RequestMapping(value="/hello",method = RequestMethod.GET)
    public String hello(Map<String,Object> map){
        map.put("name","Dear");
        return "/hello";
    }

    /**
     * 新增
     * @param userBean
     * @return
     */
    @RequestMapping("/add")
    public String add(UserBean userBean) {
        return  String.valueOf(userService.insertUser(userBean));
    }

    /**
     * 查询
     * @param map
     * @return
     */
    @RequestMapping("/findByName")
    public String findByName(Map<String,Object> map){
        String name ="zjy";
        UserBean userBean =  userService.findByName(name);
        map.put("name",userBean.getName());
        return "hello";
    }




    /**
     *测试读取配置文件中的值
     * @return
     */
    @RequestMapping("/test")
    public String test(HttpServletRequest request){
      String me =jsonUtil.messages("zjy");
      logger.info(me);
        return "hello";
    }
}
